import service from '../api/apiService.js'
import api from '../api/api.js'

export default {
    getDictionary: function (dicType, callback, returnType) {
        if (!dicType) return callback([])
        let dicValues = null
        try {
            dicValues = JSON.parse(localStorage.getItem('dictionaryMap'))[dicType]
        } catch (e) {
        }
        if (dicValues) {
            setTimeout(() => {
                queryDictionary(dicType, returnType)
            })
            return callback(convertDic(dicValues, returnType))
        }
        queryDictionary(dicType, returnType, callback)
    },
    convertDic: convertDic,
}

function queryDictionary (dicType, returnType, callback) {
    service.apiCall({
        api: api.BH999901,
        param: {
            'requestParam': {
                'reqCode': api.BH999901.code,
                'platform': 'P01',
                'data': {
                    'type': dicType
                }
            }
        },
        success: (res) => {
            let responseState = res.responseData.responseState
            if (responseState.code === 0) {
                let dicValues = res.responseData.data || []
                if (!dicValues || dicValues.length < 1) {
                    return callback()
                }
                addToDic(dicType, dicValues)
                // _this.$store.commit('addToDictionaryMap', {key: dicType, value: convertDic(dicValues, returnType)})
                if (callback) {
                    callback(convertDic(dicValues, returnType))
                }
            }
        },
        error: () => {
            if (callback) {
                callback()
            }
        }
    })
}

/* get字典 */
function getFromDic (dicType) {
    try {
        let map = JSON.parse(localStorage.getItem('dictionaryMap'))
        if (map) {
            if (dicType) {
                return map[dicType]
            }
            return map
        }
    } catch (e) {
    }
    return null
}

/* set字典 */
function addToDic (key, dicItem) {
    let dicMap = getFromDic() || {}
    dicMap[key] = dicItem
    try {
        localStorage.setItem('dictionaryMap', JSON.stringify(dicMap))
    } catch (e) {
        console.log('保存字典失败', e)
    }
}

function convertDic (dicValues, returnType) {
    if (Object.prototype.toString.call(dicValues) === '[object Array]') {
        dicValues = dicValues || []
        returnType = returnType || 'keyMap'
        if (returnType === 'keyMap') {
            let map = {}
            dicValues.filter(item => {
                map[item.value] = item.label
            })
            return map
        }
        if (returnType === 'valueMap') {
            let map = {}
            dicValues.filter(item => {
                map[item.label] = item.value
            })
            return map
        }
        if (returnType === 'list') {
            return dicValues
        }
    }
    return dicValues
}
